﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.DAL;
using SalesSys.DAL.Base;

namespace SalesSys.DAL.Models
{
    /*
     * 销售计划单相关内容实体
     */
    public abstract class SalMapper<T, TID> : BaseMap<T, TID> where T : DbEntityBase<TID>
    {

        public override string TablePrefix
        {
            get { return EntityTabConfig.SalesTabPrefix; }
        }
    }
  
   

  
    /// <summary>
    /// 非标优惠,SupplierId,DepotId
    /// </summary>
    public class NonstandardDiscount : IDbEntity
    {
        /// <summary>
        /// 钢厂品牌
        /// </summary>
        public Supplier Supplier { get; set; }

        /// <summary>
        /// 钢厂编号
        /// </summary>
        public Guid SupplierId { get; set; }

        /// <summary>
        /// 供应商
        /// </summary>
        public Provider Provider { get; set; }

        /// <summary>
        /// 仓库编号
        /// </summary>
        public Guid DepotId { get; set; }

        /// <summary>
        /// 优惠
        /// </summary>
        public decimal Discount { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime UpdateTime { get; set; }
        public class Mapper : BaseMapToCfg<NonstandardDiscount>  
        {
            public Mapper()
            {
                ToTable("sal_NonstandardDiscount");
                HasKey(p =>new{ p.SupplierId,p.DepotId});
            }
        }

        #region IDbEntity 成员

        public object[] GetKeyValues()
        {
            return new object[] { SupplierId, DepotId };
        }

        public void SetKeyValues(object[] keyValues)
        {
            SupplierId = (Guid )keyValues[0];
            DepotId = (Guid)keyValues[1];

        }

        #endregion
    }


    //public class CommodityResult
    //{
    //    public string Format { get; set; }

    //    public CommodityDto[] Commoditys { get; set; }
    //}

    //public class CommodityDto
    //{
    //    public Guid Id { get; set; }
    //    public Guid SupplierId { get; set; }
    //    /// <summary>
    //    /// 商品单重
    //    /// </summary>
    //    public decimal Weight { get; set; }

    //    /// <summary>
    //    /// 库存，千克
    //    /// </summary>
    //    public decimal Amount { get; set; }

    //    public decimal Price { get; set; }
    //    /// <summary>
    //    /// 类型,螺纹、盘圆、螺盘、钢坯
    //    /// </summary>
    //    public string Type { get; set; }
    //    /// <summary>
    //    /// 标准，300E
    //    /// </summary>
    //    public string Standard { get; set; }
    //}

  
    //[Obsolete("不在记录商品修改信息，记录商品价格库存变化历史")]
    //public class CommodityRecord : DbEntityBase<Guid>
    //{
    //    public DateTime RecordTime { get; set; }
    //    public virtual Employee Creater { get; set; }
    //    public virtual Supplier Supplier { get; set; }

    //    public string FillName()
    //    {
    //        return Type + "/" + Format + "/" + Standard;
    //    }

    //    /// <summary>
    //    /// 商品品牌，主要用于库提，如果是钢厂商品与钢厂名相同,
    //    /// 界面中应通过下拉框选择
    //    /// </summary>
    //    public string Brand { get; set; }
    //    /// <summary>
    //    /// 类型,螺纹、盘圆、螺盘、钢坯
    //    /// </summary>
    //    public string Type { get; set; }
    //    /// <summary>
    //    /// 标准，300E
    //    /// </summary>
    //    public string Standard { get; set; }
    //    /// <summary>
    //    /// 规格,直径10mm,9米
    //    /// </summary>
    //    public string Format { get; set; }

    //    /// <summary>
    //    /// 商品单重
    //    /// </summary>
    //    public decimal Weight { get; set; }

    //    public decimal Price { get; set; }

    //    /// <summary>
    //    /// 与基价差
    //    /// </summary>
    //    public decimal PriceDifference { get; set; }
    //    /// <summary>
    //    /// 库存，千克
    //    /// </summary>
    //    public decimal Amount { get; set; }


    //    public class Mapper : SalMapper<CommodityRecord, Guid>
    //    {
    //    }

    //}

 //   public class Order : DbEntityBase<Guid>
 //   {
   
 //       public class Mapper : SalMapper<Order, Guid>
 //       {
 //           public Mapper()
 //           {
 //               HasMany(p => p.Records).WithOptional().WillCascadeOnDelete();
 //           }
 //       }
 //       public virtual Customer Customer { get; set; }
 //       public virtual District District { get; set; }
       
 //       public string Address { get; set; }
 ///// <summary>
 //       /// 创建人，一般为客户专员
 //       /// </summary>
 //       public virtual Employee Creater { get; set; }
 //       public DateTime CreateTime { get; set; }
 //       /// <summary>
 //       /// 是否标件
 //       /// </summary>
 //       public bool IsStandard { get; set; }

 //       /// <summary>
 //       /// 标件户头
 //       /// </summary>
 //       public string Account { get; set; }
 //       /// <summary>
 //       /// 是否先款
 //       /// </summary>
 //       public bool IsFullPayment { get; set; }

 //       /// <summary>
 //       /// 是否送货
 //       /// </summary>
 //       public bool IsDelivery { get; set; }
 //       /// <summary>
 //       /// 运费
 //       /// </summary>
 //       public decimal DeliveryPrice { get; set; }
 //       /// <summary>
 //       /// 车辆信息
 //       /// </summary>
 //       public string VehicleInfo { get; set; }
 //       public string Remarks { get; set; }

      
 //       /// <summary>
 //       /// 供货者
 //       /// </summary>
 //       public virtual Supplier Supplier { get; set; }

 //       public virtual ICollection<CommodityInfo> CommodityInfos { get; set; }

 //       public OrderType OrderType { get; set; }

 //       public decimal SourceWeight { get; set; }
 //       /// <summary>
 //       /// 总重
 //       /// </summary>
 //       public decimal Weight { get; set; }
 //       /// <summary>
 //       /// 总价
 //       /// </summary>
 //       public decimal Total { get; set; }

 //       public decimal SourceTotal { get; set; }
 //       /// <summary>
 //       /// 直接优惠合计，优惠使用负值表示
 //       /// </summary>
 //       public decimal DiscountAmount { get; set; }
 //       /// <summary>
 //       /// 每吨优惠合计,优惠使用负值表示
 //       /// </summary>
 //       public decimal UnitDiscount { get; set; }
 //       /// <summary>
 //       /// 根据商品信息重新计算重量及价格,优惠也会计算在内
 //       /// </summary>
 //       public void CalcNew()
 //       {
 //           if (CommodityInfos==null)
 //           {
 //               SourceWeight = 0;
 //               Weight = 0;
 //               Total = 0;
 //               SourceTotal = 0;
 //               DiscountAmount = 0;
 //           }
 //           else
 //           {
 //               SourceWeight = CommodityInfos.Sum(p => p.SourceWeight);
 //               Weight = CommodityInfos.Sum(p => p.Weight);
 //               Total = CommodityInfos.Sum(p => p.Price*p.Weight)+Weight*UnitDiscount+DiscountAmount;
 //               SourceTotal = CommodityInfos.Sum(p =>p.SourcePrice* p.SourceWeight);
 //           }
 //       }

 //       public LockPriceList LockPriceList { get; set; }

 //       /// <summary>
 //       /// 锁价基价，只对基价锁价单有效
 //       /// </summary>
 //       [Obsolete("准备删除")]
 //       public decimal BaseLockPrice { get; set; }
 //       /// <summary>
 //       /// 锁价总重，只对基价锁价单有效
 //       /// </summary>
 //            [Obsolete("准备删除")]

 //       public decimal BaseLockWeight { get; set; }
        
        
 //       /// <summary>
 //       /// 锁价单是否已激活为计划单,待确认、取消、通过、已转订单
 //       /// </summary>
 //       [Obsolete("使用LockPriceState控制状态")]
 //       public bool IsLockActivation { get; set; }
 //       [Obsolete("准备删除")]

 //       public LockPriceState LockPriceState { get; set; }
 //       /// <summary>
 //       /// 是否议价审批中
 //       /// </summary>
 //       public bool IsApproval { get; set; }

 //       /// <summary>
 //       /// 执行状态
 //       /// </summary>
 //       public OrderState State { get; set; }
 //       /// <summary>
 //       /// 是否暂停
 //       /// </summary>
 //       public bool IsPause { get; set; }

 //       /// <summary>
 //       /// 订单变化记录
 //       /// </summary>
 //       public virtual ICollection<OrderRecord> Records { get; set; }
 //   }
    /// <summary>
    /// 锁价单状态
    /// </summary>
    public enum LockPriceState
    {
        [Description("待确认")]
        Pending = 0,
        [Description("取消")]
        Cancel = 1,
        [Description("已确认")]
        Confirm = 2,
        [Description("已激活")]
        ToOrder = 3,
    }
   /// <summary>
   /// 议价申请
   /// </summary>
    public class PriceApproval : DbEntityBase<Guid>
    {
        /// <summary>
        /// 计划单
        /// </summary>
        public virtual Order Order { get; set; }
        /// <summary>
        /// 申请单价优惠金额,优惠使用负值表示
        /// </summary>
        public decimal ApplyDiscount { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 操作时间
        /// </summary>
        public DateTime? HandleTime { get; set; }
        /// <summary>
        /// 申请人
        /// </summary>
        public virtual Employee Applicant { get; set; }

        /// <summary>
        /// 操作人
        /// </summary>
        public virtual Employee Handler { get; set; }

        /// <summary>
        /// 申请备注
        /// </summary>
        public string ApplyRemark { get; set; }

        /// <summary>
        /// 处理意见
        /// </summary>
        public string HandleRemark { get; set; }

        /// <summary>
        /// 审批状态
        /// </summary>
        public ApprovalState State { get; set; }
        /// <summary>
        /// 订单已出库，已生成出库
        /// </summary>
    
        public class Mapper : SalMapper<PriceApproval, Guid>
        {
            public Mapper()
            {
               
            }
        }
    }
        
    /// <summary>
    /// 订单状态
    /// </summary>
    public enum OrderState
    {
        /// <summary>
        /// 待确认
        /// </summary>
        [Description("待确认")]
        Pending = 0,

        /// <summary>
        /// 驻厂确认
        /// </summary>
         [Description("驻厂确认")]
        Ordered=1,

        /// <summary>
        /// 已发货
        /// </summary>
        [Description("已发货")]
        Send = 2,

        /// <summary>
        /// 已确认收货
        /// </summary>
        [Description("已收货")]
        Confirmed = 3,

        /// <summary>
        /// 已结算，需要通过IsSettlement来进行判断
        /// </summary>
        [Description("已结算")]
        Settlement = 4,

        /// <summary>
        /// 订单完结
        /// </summary>
        [Description("完结")]
        End = 5,

        /// <summary>
        /// 取消
        /// </summary>
        [Description("取消")]
        Cancel = 6,

        /// <summary>
        /// 审核通过
        /// </summary>
        [Description("审核通过")]
        Checked = 7,

        /// <summary>
        /// 已退货
        /// </summary>
        [Description("已退货")]
        Return = 8,

        /// <summary>
        /// 已退款
        /// </summary>
        [Description("已退款")]
        Refund = 9
        
    }

    /// <summary>
    /// 订单类型
    /// </summary>
    public enum OrderType
    {
        /// <summary>
        /// 普通订单,直发销售
        /// </summary>
        [Description("普通订单")]
        Normal = 0,

        /// <summary>
        /// 商品锁价
        /// </summary>
        [Description("商品锁价")]
        ProductLock = 1,

        /// <summary>
        /// 基价锁定:客户、基价、重量
        /// </summary>
        [Description("基价锁价")]
        BasePriceLock = 2,

        /// <summary>
        /// 自采订单,贸易商采购:客户、总重，总价，客户经理，备注
        /// </summary>
        [Description("自采订单")]
        Freedom = 3,
        /// <summary>
        /// 仓库销售
        /// </summary>
            [Description("库提销售")]
        Depot = 4
    }

    /// <summary>
    /// 流程状态
    /// </summary>
    public enum ProcessState
    {
        /// <summary>
        /// 正常
        /// </summary>
        [Description("正常")]
        Normal = 0,

        /// <summary>
        /// 议价
        /// </summary>
        [Description("议价")]
        Bargaining = 1,

        /// <summary>
        /// 锁价
        /// </summary>
        [Description("锁价")]
        LockPrice = 2,

        /// <summary>
        /// 款项确认
        /// </summary>
        [Description("款项确认")]
        FullPayment = 3,

        /// <summary>
        /// 审核通过
        /// </summary>
        [Description("审核通过")]
        Checked = 4
        //,
        ///// <summary>
        ///// 驻场接单
        ///// </summary>
        //[Description("驻场接单")]
        //DockingOrder = 4,

        ///// <summary>
        ///// 财务审批
        ///// </summary>
        //[Description("结算")]
        //Settlement = 5,

    }

    

    

    ///// <summary>
    ///// 订单商品信息
    ///// </summary>
    //public class CommodityInfo : DbEntityBase<Guid>
    //{
    //    public class Mapper : SalMapper<CommodityInfo, Guid>
    //    {
    //        public Mapper()
    //        {
    //            HasMany(p => p.Records).WithOptional().WillCascadeOnDelete();
    //        }
    //    }
    //    public virtual Commodity Commodity { get; set; }
    //    public int Count { get; set; }
    //    public decimal Price { get; set; }
    //    /// <summary>
    //    /// 总重
    //    /// </summary>
    //    public decimal Weight { get; set; }

    //    public decimal SourcePrice { get; set; }
    //    public decimal SourceWeight { get; set; }
    //    public virtual ICollection<OrderRecord> Records { get; set; }
    //    /// <summary>
    //    /// 所属订单
    //    /// </summary>
    //    public virtual Order Order { get; set; }
    //    /// <summary>
    //    /// 用于新建商品信息，通过商品和数量，计算其它值
    //    /// </summary>
    //    public void CalcNew()
    //    {
    //        SourcePrice = Commodity.Price;
    //        SourceWeight = Commodity.Weight*Count;
    //        Price = SourcePrice;
    //        Weight = SourceWeight;
    //    }

    //}
    /// <summary>
    /// 订单变化记录
    /// </summary>
    public class OrderRecord : DbEntityBase<Guid>
    {
        public OrderRecord()
        {
            CreateTime = DateTime.Now;
        }

        public class Mapper : SalMapper<OrderRecord, Guid>
        {
        }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 订单先款优惠、非标优惠、调价；商品调价、商品调重
        /// </summary>
        public DiscountType DiscountType { get; set; }
        /// <summary>
        /// 变化价，如：+5，-5
        /// </summary>
        public decimal Price { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks { get; set; }
        /// <summary>
        /// 相关数据id
        /// </summary>
        public string LinkId { get; set; }
        /// <summary>
        /// 自定义数据标记
        /// </summary>
        public string Tag { get; set; }
    }

    public enum DiscountType
    {
        /// <summary>
        /// 消息记录
        /// </summary>
        [Description("消息记录")]
        Msg = 0,
        /// <summary>
        /// 先款
        /// </summary>
        [Description("先款")]
        Payment = 1,
        /// <summary>
        /// 非标
        /// </summary>
        [Description("非标")]
        Nonstandard = 2,
        /// <summary>
        /// 价格变化
        /// </summary>
        [Description("价格变化")]
        PriceChange = 3,
        /// <summary>
        /// 重量变化
        /// </summary>
        [Description("重量变化")]
        WeightChange = 4,
        /// <summary>
        /// 审批优惠
        /// </summary>
        [Description("审批优惠")]
        Approval = 5,
        /// <summary>
        /// 订单状态变化
        /// </summary>
         [Description("状态变化")]
        StateChange=6,
        /// <summary>
        /// 基本信息修改
        /// </summary>
         [Description("基本信息")]
        BaseInfo=7

    }

   

}
